package com.zyg.shop.controller;

import com.zyg.common.utils.FastDFSClient;
import com.zyg.common.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * 功能：文件操作
 * 作者： fufeixiang
 * 日期： 2021-11-29
 */
@RestController
@RequestMapping("/shop")
public class FileController {

    //文件上传
    @PostMapping("/image/upload")
    public R upload(MultipartFile file) throws Exception {
        FastDFSClient fastDFSClient = new FastDFSClient("classpath:fastdfs.conf");
        //获取文件名
        String fileName = file.getOriginalFilename();
        //获取文件扩展名
        String extr = fileName.substring(fileName.lastIndexOf('.') + 1);
        //上传文件
        String s = fastDFSClient.uploadFile(file.getBytes(), extr);
        //文件路径
       String url = "http://192.168.35.132:8080/" + s;
        //返回文件路径
        return R.ok().put("url",url);
    }
}
